package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;

@RequestMapping("/admin/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页查询菜品
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> dishPageQuery(DishPageQueryDTO dto) {
        PageResult page = dishService.dishPageQuery(dto);
        return Result.success(page);
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveDish(@RequestBody DishDTO dto) {
        
        dishService.saveDish(dto);
        deleteDish("dish_"+dto.getCategoryId());
        return Result.success();
    }

    private void deleteDish(String key) {

       Set<String> parret =  redisTemplate.keys(key);
        redisTemplate.delete(parret);
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids) {
        dishService.deleteDish(ids);
        deleteDish("dish_*");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Integer id) {
        DishVO dish = dishService.getById(id);
        return Result.success(dish);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result updataDish(@RequestBody DishDTO dto) {
        dishService.updataDish(dto);
        deleteDish("dish_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品的启售禁售")
    private Result status(@PathVariable Integer status,Integer id){
     dishService.status(status,id);
        deleteDish("dish_*");
     return Result.success();
    }

     @GetMapping("list")
       private Result<List<DishVO>> getDishList(){
         List<DishVO> dishVOList = dishService.getDishList();
         return Result.success(dishVOList);
     }

}
